<template>
  <div>
    <!-- 按钮组 -->
    <el-button-group style="margin-bottom: 15px">
      <el-button type="primary" size="small" @click="addQuestion">新增问题</el-button>
    </el-button-group>
    <!-- 表格 -->
    <el-table border :data="questions" stripe max-height="500" style="width: 100%">
      <el-table-column label="#" type="index" width="50"> </el-table-column>
      <el-table-column prop="title" label="题目"> </el-table-column>
      <el-table-column prop="answers" label="答案" width="180"> </el-table-column>
      <el-table-column prop="cid" width="180" label="分类"> </el-table-column>
      <el-table-column prop="created" width="180" label="发布时间"> </el-table-column>

      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="info" size="small">详情</el-button>
          <el-button type="warning" size="small">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 抽屉 -->
    <el-drawer title="我是标题" size="50%" :visible.sync="drawer" :with-header="false">
      <div style="padding: 20px">
        <el-page-header @back="drawer = false" title="关闭"> </el-page-header>
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="100px"
          class="demo-form"
        >
          <el-form-item label="题目" prop="title">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入内容"
              v-model="form.title"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="分类" prop="cid">
            <el-select v-model="form.cid" placeholder="请选择" size="small">
              <el-option
                v-for="(item, key) in classifyMap"
                :key="key"
                :label="item"
                :value="key"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选项" prop="options">
            <el-checkbox-group v-model="form.answers">
              <div class="options" v-for="(item, index) in form.options" :key="index">
                <el-checkbox :label="getLetterByIndex(index)"></el-checkbox>
                <el-input
                  type="textarea"
                  autosize
                  placeholder="请输入内容"
                  v-model="item.value"
                >
                </el-input>
                <template v-if="index == form.options.length - 1">
                  <el-button
                    @click="addOptions"
                    type="success"
                    icon="el-icon-plus"
                    circle
                  ></el-button>
                  <el-button
                    :title="index"
                    @click="delOptions(index)"
                    type="danger"
                    v-if="index > 3"
                    icon="el-icon-minus"
                    circle
                  ></el-button>
                </template>
              </div>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="答案" prop="answers">
            <div>{{ doSort(form.answers).join(",") }}</div>
          </el-form-item>
          <el-form-item label="答案提示" prop="tips">
            <el-input
              v-model="form.tips"
              placeholder="请输入内容"
              type="textarea"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="handleSave">保存</el-button>
            <el-button size="small" @click="drawer = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
export default {
  layout: "main",
  data() {
    return {
      form: {
        uid: 1,
        answers: [],
        options: [
          {
            value: "",
          },
          {
            value: "",
          },
          {
            value: "",
          },
          {
            value: "",
          },
        ],
      },
      rules: {
        title: [{ required: true, message: "请输入题目", trigger: "blur" }],
        cid: [{ required: true, message: "请选择活动区域", trigger: "change" }],
        options: [{}],
        answers: [{ required: true, message: "请输入内容", trigger: "blur" }],
        tips: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      drawer: false,
    };
  },
  fetch({ $axios, store }) {
    store.dispatch("exam/getQuestionData", $axios);
    store.dispatch("classify/getClaccifyData", $axios);
  },
  computed: {
    ...mapState("exam", ["questions"]),
    ...mapGetters("classify", ["classifyMap"]),
  },
  methods: {
    // 删除选项
    delOptions() {
      this.form.options.splice(index, 1);
    },
    // 增加选项
    addOptions() {
      this.form.options.push({ value: "请输入内容" });
    },
    getLetterByIndex(index) {
      return String.fromCharCode("A".charCodeAt(0) + index);
    },
    doSort(arr) {
      return [...arr].sort();
    },
    addQuestion() {
      this.drawer = !this.drawer;
    },
    handleSave() {
      this.$store.dispatch("exam/addClassifyData", {
        $axios: this.$axios,
        form: this.form,
      });
      this.drawer = false;
    },
    handleDel(id){
      console.log(id)
    }
  },
};
</script>

<style scoped>
.options {
  display: flex;
  align-items: center;
}
.el-checkbox:last-of-type {
  margin-right: 15px;
}
.options .el-textarea {
  width: 80%;
  margin-right: 15px;
}
</style>
